$(function () {
    //======================
    //======设置查询条件========
    //======================
    //内部公司
    $("[name='gyswl_gs']").jgbmtree($("[name='gyswl_bm']"), '', '', '');
    // $("[name='gyswl_gys']").qyjgzcombobox();
    //搜索按钮
    $("#gyswl_toolbar [name='searchButton']").click(function () {
        $(grid).datagrid({
            queryParams:{
                gsid:$("#gyswl_toolbar [name='gyswl_gs']").val(),
                deptid:$("#gyswl_toolbar [name='gyswl_bm']").val(),
                enddt:$("#gyswl_toolbar [name='gyswl_endDate']").val(),
                custname:$("#gyswl_toolbar [name='gyswl_gys']").val(),
                tc_flag:$("#gyswl_toolbar [name='gyswl_tc']").val()
            }});
    });


//======================
//========统计列表========
//======================
    var columns = [
        [
            {field:'gs_name', title:'公司名称', rowspan:2, width:145, align:'center'},
            {field:'cust_name', title:'供应商', rowspan:2, width:250, align:'center'},
            {title:'业务', colspan:2, width:100, align:'right'},
            {title:'财务', colspan:5, width:100, align:'right'}
        ],
        [
            {field:'yw_yufk', title:'预付款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='yw_yufk' gsid='{1}' custname='{2}'>{3}</a>".stringFormat(color, r.gs_id, r.cust_name, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'yw_yingfk', title:'应付款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='yw_yingfk' gsid='{1}' custname='{2}'>{3}</a>".stringFormat(color, r.gs_id, r.cust_name, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'cgwjs_sl', title:'采购未结算数量', width:145, formatter:function (v, r, i) {
                return formatSl2(v, ",", ".");
            }, align:'right'},
            {field:'cgwjs_je', title:'采购未结算金额', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='cgwjsje' gsid='{1}' custname='{2}'>{3}</a>".stringFormat(color, r.gs_id, r.cust_name, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'wlwjs_je', title:'物流未结算金额', align:'right', formatter:function (v, r, i) {
                return formatSl2(v, ",", ".");
            }, width:145},
            {field:'cw_yufk', title:'预付款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='cw_yufk' gsid='{1}' custname='{2}'>{3}</a>".stringFormat(color, r.gs_id, r.cust_name, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'cw_yingfk', title:'应付款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='cw_yingfk' gsid='{1}' custname='{2}'>{3}</a>".stringFormat(color, r.gs_id, r.cust_name, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'}
        ]
    ];

    var grid = $('#gyswl_grid').datagrid({
        url:$.webPath() + '/tjbb/gyswl/gyswlList',
        toolbar:"#gyswl_toolbar",
        pagination:true,
        pageSize:100,
        rownumbers:true,
        singleSelect:true,
        collapsible:true,
        fit:true,
        fitColumns:true,
        pageNumber:1,
        showFooter:true,
        queryParams:{
            gsid:$("#gyswl_toolbar [name='gyswl_gs']").val(),
            deptid:$("#gyswl_toolbar [name='gyswl_bm']").val(),
            enddt:$("#gyswl_toolbar [name='gyswl_endDate']").val(),
            custname:$("#gyswl_toolbar [name='gyswl_gys']").val(),
            tc_flag:$("#gyswl_toolbar [name='gyswl_tc']").val()
        },
        onLoadSuccess:function () {  //cgwjsje
            $(".cgwjsje").unbind("click").bind("click", function () {
                gyswlDzShowDetail1('采购未结算金额', $.webPath() + "/tjbb/gyswl/cgwjsje", $(this).attr("gsid"), $(this).attr("custname"), 1);
            });
            $(".yw_yufk").unbind("click").bind("click", function () {
                gyswlDzShowDetail('供应商往来对账', $.webPath() + "/tjbb/gyswl/gyswldz", $(this).attr("gsid"), $(this).attr("custname"), 1);
            });
            $(".yw_yingfk").unbind("click").bind("click", function () {
                gyswlDzShowDetail('供应商往来对账', $.webPath() + "/tjbb/gyswl/gyswldz", $(this).attr("gsid"), $(this).attr("custname"), 1);
            });
            $(".cw_yufk").unbind("click").bind("click", function () {
                gyswlDzShowDetail('供应商往来对账', $.webPath() + "/tjbb/gyswl/gyswldz", $(this).attr("gsid"), $(this).attr("custname"), 2);
            });
            $(".cw_yingfk").unbind("click").bind("click", function () {
                gyswlDzShowDetail('供应商往来对账', $.webPath() + "/tjbb/gyswl/gyswldz", $(this).attr("gsid"), $(this).attr("custname"), 2);
            });

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        columns:columns
    });

    //===========================================
//==============此页面用到的函数===================
//===========================================

    var gyswlDzShowDetail = function (title, url, gsid, custname, type) {
        var win = $.window({
            title:title,
            width:1050,
            height:470,
            cache:false,
            iconCls:'icon-tip',
            url:encodeURI(encodeURI(url + "?gsid=" + gsid + "&custname=" + custname + "&type=" + type + "&enddt=" + $("#gyswl_endDate").datebox("getValue") + "&deptid=" + $("#gyswl_toolbar input[name='gyswl_bm']").val())),
            modal:true,
            buttons:[]
        });
    }
    var gyswlDzShowDetail1 = function (title, url, gsid, custname, enddt) {
        var win = $.window({
            title:title,
            width:1050,
            height:470,
            cache:false,
            iconCls:'icon-tip',
            url:encodeURI(encodeURI(url + "?gsid=" + gsid + "&custname=" + custname + "&enddt=" + $("#gyswl_endDate").datebox("getValue") + "&deptid=" + $("#gyswl_toolbar input[name='gyswl_bm']").val())),
            modal:true,
            buttons:[]
        });
    }

    //=========================导出=================================
    $("#btn_gyswl_export").click(function () {
        window.open($.webPath() + "/tjbb/gyswl/gyswl_export?gsid=" + $("#gyswl_toolbar [name='gyswl_gs']").val() + "&deptid=" + $("#gyswl_toolbar [name='gyswl_bm']").val() + "&enddt=" + $("#gyswl_toolbar [name='gyswl_endDate']").val() +
            "&custname=" + $("#gyswl_toolbar [name='gyswl_gys']").val() +
            "&tc_flag=" + $("#gyswl_toolbar [name='gyswl_tc']").val());
    });
    //==================================================================================
});
